package com.lenovo.manufacture;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class SaleActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private TextView car_all_price;
    private TextView car_price_1;
    private TextView car_price_2;
    private TextView car_price_3;
    private ListView sale_list_view;
    private SaleAdapter saleAdapter;
    public static final Random RANDOM = new Random();
    private List<SellCar> sellCarList;
    public static final String URL_SALE = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale);
        getResponse();
        sellCarList = new ArrayList<SellCar>();
        addToList();
        saleAdapter = new SaleAdapter(sellCarList, this);
        initView();
        sale_list_view.setAdapter(saleAdapter);
        sale_list_view.setOnItemClickListener(this);
    }

    private void getResponse() {
        RequestQueue queue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST,
                URL_SALE,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                }
        );
        queue.add(jsonObjectRequest);
    }

    //添加对象至list
    private void addToList() {
        sellCarList.add(new SellCar("蓝色汽车", 600, stringToDate("2019-12-06"), 16));
        sellCarList.add(new SellCar("红色汽车", 780, stringToDate("2019-12-05"), 30));
        sellCarList.add(new SellCar("白色汽车", 1230, stringToDate("2019-12-07"), 12));
        sellCarList.add(new SellCar("黑色汽车", 6300, stringToDate("2019-12-02"), 25));
        sellCarList.add(new SellCar("紫色汽车", 656, stringToDate("2019-12-06"), 12));
        sellCarList.add(new SellCar("青色汽车", 400, stringToDate("2019-11-29"), 45));
        sellCarList.add(new SellCar("黄色汽车", 1200, stringToDate("2019-11-23"), 78));
        sellCarList.add(new SellCar("绿色汽车", 436, stringToDate("2019-12-07"), 67));
    }

    //将传入的String转换为Date
    private Date stringToDate(String string) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(string);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    //实例化控件
    private void initView() {
        car_all_price = (TextView) findViewById(R.id.car_all_price);
        car_price_1 = (TextView) findViewById(R.id.car_price_1);
        car_price_2 = (TextView) findViewById(R.id.car_price_2);
        car_price_3 = (TextView) findViewById(R.id.car_price_3);
        sale_list_view = (ListView) findViewById(R.id.sale_list_view);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int allPrice = sellCarList.get(position).getPrice() * sellCarList.get(position).getSellNum();
        car_all_price.setText(allPrice + "");
        car_price_1.setText(allPrice + "");
        car_price_2.setText(allPrice + "");
        car_price_3.setText(allPrice + "");
    }
}
